package com.starlight.easybuyprictice.service;

import com.starlight.easybuyprictice.mapper.GoodsCategoryMapper;
import com.starlight.easybuyprictice.entity.MsGoodsCat;
import com.starlight.easybuyprictice.entity.brief.NestedCategory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author yfy2001
 * @date 2022/11/29 12:41
 */
@Service
public class GoodsManageService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;


    public GoodsManageService setGoodsCategoryDao(GoodsCategoryMapper goodsCategoryMapper) {
        this.goodsCategoryMapper = goodsCategoryMapper;
        return this;
    }

    //@Autowired
//private GoodsDao goodsDao;
    public Result listParentCategories() {
        Result result = new Result();
        List<NestedCategory> goodsCategories = goodsCategoryMapper.findParents();
        result.setData(goodsCategories);
//System. out.println(result.getData());
        return result;
    }

    public Result updateGoodsCategories(int parentId, String oldName, String newName) {
        Result result = new Result();
        if (StringUtils.isEmpty(oldName)) {
            MsGoodsCat targetCategory = goodsCategoryMapper.findByName(newName);
            if (targetCategory == null) {
                targetCategory = new MsGoodsCat();
                targetCategory.setParentId(parentId);
                targetCategory.setName(newName);
                int affectedRowCount = goodsCategoryMapper.save(targetCategory);
                if (affectedRowCount == 1) {
                    result.setStatus(0);
                    result.setMsg("新增分类成功");
                } else {
                    result.setMsg("异常情况导致新增分类失败");
                }
            } else {
                result.setMsg("当前分类名称已使用，新增分类失败");
            }
        } else if (oldName.equals(newName)) {
            MsGoodsCat modifyingCategory = new MsGoodsCat();
            modifyingCategory.setName(newName);
            modifyingCategory.setParentId(parentId);
            int affectedRowCount = goodsCategoryMapper.modify(modifyingCategory);
            if (affectedRowCount == 1) {
                result.setStatus(1);
                result.setMsg("修改当前分类成功");
            } else {
                result.setMsg("异常情况导致当前分类修改失败");
            }
        } else {
            MsGoodsCat targetCategory = goodsCategoryMapper.findByName(oldName);
            targetCategory.setParentId(parentId);
            targetCategory.setName(newName);
            targetCategory.setModifytime(new Date());
            int affectedRowCount = goodsCategoryMapper.update(targetCategory);
            if (affectedRowCount == 1) {
                result.setStatus(1);
                result.setMsg("修改分类成功");
            } else {
                result.setMsg("异常情况修改当前分类失败");
            }
        }
        return result;
    }

    public Result listNestedGoodCategories() {
        Result result = new Result();
        List<NestedCategory> categories = goodsCategoryMapper.findNests();
        result.setData(categories);
        return result;
    }

    public Result deleteCategory(int catId) {
        Result result = new Result();
        int affectedRowCount = goodsCategoryMapper.deleteById(catId);
        if (affectedRowCount == 1) {
            result.setStatus(0);
            result.setMsg("删除分类成功");
        } else {
            result.setMsg("册除分类失败");
        }
        return result;
    }

    //查找子类分类
    public Result listChildrenGoodCategories(){
        Result result = new Result();
        List<MsGoodsCat> categories = goodsCategoryMapper.findAllChildrenCategory();
        result.setData(categories);
        return result;
    }

    public Result addCategory(String name, Integer parentId) {
        Result result=new Result();
        MsGoodsCat msGoodsCat=new MsGoodsCat();
        msGoodsCat.setName(name);
        msGoodsCat.setParentId(parentId);
        msGoodsCat.setCreatetime(new Date());
        msGoodsCat.setModifytime(new Date());
        if(goodsCategoryMapper.insertNewCat(msGoodsCat)==1){
            result.setStatus(0);
            result.setMsg("添加成功");
        }
        else {
            result.setStatus(2);
            result.setMsg("添加失败");
        }
        return result;
    }
}



